package com.hmyd.hmyd_android.shop;

import java.lang.reflect.Type;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.DtoInfoNoPage;
import com.hmyd.hmyd_android.dto.InfoDto;
import com.hmyd.hmyd_android.dto.Message;
import com.hmyd.hmyd_android.dto.ShopMessageDetail;
import com.hmyd.hmyd_android.ui.BasicActivity;
import com.hmyd.hmyd_android.utils.MyToast;
import com.hmyd.hmyd_android.utils.Utils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

/**
 * 消息详情
 * @author wangk
 *
 */
@ContentView(R.layout.activity_message_detail)
public class JfMallMessageDetailActivity extends BasicActivity {
	private TextView mTvTitle;
	private TextView mTvSender;
	private TextView mTvTime;
	private TextView mTvContent;
	private static final int MESSAGE_DETAIL = 1;
	private final int READED = 2;
	
	private Message message;
	
	
	@ViewInject(R.id.home_title)
	private View home_title;
	@ViewInject(R.id.home_title_txt)
	private TextView home_title_txt;
	
	public static Intent getIntent(Context context,Message message) {
		Intent intent = new Intent();
		intent.setClass(context, JfMallMessageDetailActivity.class);
		intent.putExtra("message", message);
		return intent;
	}

	@Override
	protected void onResume() {
		super.onResume();

		RequestParams params = new RequestParams();
		params.addBodyParameter("messageId", message.messageId);

		onRequest(MESSAGE_DETAIL, UrlConstant.SHOP_MESSAGE_DETAIL, HttpMethod.GET, params);
		onRequest(READED, UrlConstant.READED_SHOP_MESSAGE, HttpMethod.POST, params);

	}

	@Override
	public void onPostSuccess(int postId, String result) {
		Gson gson = new Gson();
		if (postId == READED) {
			DtoInfoNoPage<InfoDto> dtoInfoNoPage1 = new DtoInfoNoPage<InfoDto>();
			Type type1 = new TypeToken<DtoInfoNoPage<InfoDto>>() {
			}.getType();
			dtoInfoNoPage1 = gson.fromJson(result, type1);
			if(dtoInfoNoPage1 == null || dtoInfoNoPage1.suc != 1011) {
				MyToast.showHMToast(JfMallMessageDetailActivity.this, false, "设置已读失败");
			}
			return;
		}
		DtoInfoNoPage<ShopMessageDetail> dtoInfoNoPage2 = new DtoInfoNoPage<ShopMessageDetail>();
		Type type2 = new TypeToken<DtoInfoNoPage<ShopMessageDetail>>() {
		}.getType();
		dtoInfoNoPage2 = gson.fromJson(result, type2);
		if(dtoInfoNoPage2 != null) {
			ShopMessageDetail messageDetail = dtoInfoNoPage2.data;

			mTvContent.setText(messageDetail.subject);
			mTvSender.setText(messageDetail.sender);
			mTvTitle.setText(messageDetail.sender);
			
			mTvTime.setText(Utils.getDateString(message.sendTime*1000));
		} else {
			MyToast.showHMToast(JfMallMessageDetailActivity.this, false, "拉取消息详情失败");
		}
	}

	@Override
	protected void setParams() {
		home_title.setOnTouchListener(touch);
		home_title_txt.setText(getString(R.string.me_message_detail));
		message = (Message) getIntent().getSerializableExtra("message");
		if(message == null) {
			AppBasic.getInstance().finishActivity(this);
		}
//		sender = getIntent().getStringExtra("sender");
//		time = getIntent().getLongExtra("time", 0);

//		mContext = this;
		mTvTitle = (TextView) findViewById(R.id.tv_title);
		mTvTime = (TextView) findViewById(R.id.tv_time);
		mTvContent = (TextView) findViewById(R.id.tv_content);
		mTvSender = (TextView) findViewById(R.id.tv_sender);
		
		setResult(RESULT_OK);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK) {
			AppBasic.getInstance().finishCurrentActivity();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}


	@Override
	protected void leftButton() {
		AppBasic.getInstance().finishCurrentActivity();
	}

	@Override
	protected void rightButton() {

	}

	@Override
	public void onPostFailure(int postId, String msg) {
		
	}

}
